//
//  ChatCell.m
//  YouinLiveDemo
//
//  Created by lprison on 2021/9/5.
//

#import "ChatTextCell.h"



@implementation ChatTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self iniUI];
    }
    return self;
}

-(void)iniUI{
    self.avatar = [UIImageView new];
    self.avatar.layer.cornerRadius = 20;
    self.avatar.layer.masksToBounds = YES;
    
    self.nameLabel = [UILabel new];
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    
    self.timeLabel = [UILabel new];
    self.timeLabel.font = [UIFont systemFontOfSize:11];
    self.timeLabel.textColor = [UIColor grayColor];
    
    self.content = [UIView new];
    self.content.backgroundColor = [UIColor lightGrayColor];
    
    self.text = [UILabel new];
    self.text.numberOfLines = 0;
    self.text.font = [UIFont systemFontOfSize:14];
    
   
    
    [self.contentView addSubview:self.avatar];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.content];
    [self.content addSubview:self.text];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.left.mas_equalTo(self.contentView).offset(16);
        make.top.mas_equalTo(self.contentView).offset(5);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.avatar.mas_right).offset(5);
        make.top.mas_equalTo(self.avatar);
        
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView).offset(-16);
        make.top.mas_equalTo(self.avatar);

    }];
    
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.avatar).offset(35);
        make.left.mas_equalTo(self.avatar.mas_right).offset(5);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
    }];
    
    [self.text mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.content).offset(5);
        make.bottom.mas_equalTo(self.content).offset(-5);
        make.left.mas_equalTo(self.content).offset(10);
        make.right.mas_equalTo(self.content).offset(-10);
        make.width.lessThanOrEqualTo(self.contentView.mas_width).offset(-92);
    }];
    
    
    
    [self layoutIfNeeded];
    
    //[self.content addCorner:UIRectCornerTopRight|UIRectCornerBottomLeft|UIViewContentModeBottomRight radius:5];

}

-(void)setMessage:(BaseMessageModel *)message{
  
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:message.avatar]];
    self.nameLabel.text = message.nickName;
    self.timeLabel.text = [self timestampSwitchTime:message.sendTime andFormatter:@"YYYY-MM-dd hh:mm:ss"];
    self.text.text = message.text;
    
    [self layoutIfNeeded];
}

- (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    if ([format isEqual:@"HH:mm:ss"] && confromTimespStr.length<8) {
        confromTimespStr = @"00:00:00";
    }
    if ([format isEqual:@"YYYY-MM-dd HH:mm:ss"] && confromTimespStr.length<18) {
        confromTimespStr = @"2000-01-01 00:00:00"; //默认返回
    }
    return confromTimespStr;
}

@end
